#ifndef __UM_SHARED_COUNT_H
#define __UM_SHARED_COUNT_H

#include "sp_counted_impl.hpp"

class shared_count
{
private:
	sp_counted_base* pi;

public:

	shared_count(void) : pi(0) {
	}

	template <class _Ptr>
	shared_count(_Ptr p) : pi(0) {
		pi = new sp_counted_impl_p<_Ptr>(p);
		if (pi == 0) {
			delete p;
		}
	}

	~shared_count() {
		if (pi != 0) {
			pi->release();
		}
	}

	shared_count(const shared_count& other) : pi(other.pi) {
		if (pi != 0) {
			pi->add_ref_copy();
		}
	}

	void swap(shared_count& other) {
		sp_counted_base* tmp = other.pi;
		other.pi = this->pi;
		this->pi = tmp;
	}

	long use_count() const {
		return this->pi == 0 ? 0 : pi->use_count();
	}

	bool unique() const {
		return use_count() == 1;
	}

	bool empty() const {
		return this->pi == 0;
	}

	friend inline bool operator==(shared_count const & a, shared_count const & b) {
		return a.pi == b.pi;
    }

    friend inline bool operator<(shared_count const & a, shared_count const & b) {
        //return std::less<sp_counted_base*>()(a.pi, b.pi);
		return a.pi < b.pi;
    }

};


#endif // __UM_SHARED_COUNT_H